﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using HostTool.Core;
using HostTool.Protocol;
using System.Threading;

namespace HostTool
{
    public partial class FormMonitor : Form
    {

        Thread threadRxMonitor;
        public FormMonitor()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            threadRxMonitor = new Thread(new ThreadStart(ProcMonitorRx));
            threadRxMonitor.IsBackground = true;
            threadRxMonitor.Start();
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);
            if (threadRxMonitor != null && threadRxMonitor.IsAlive)
            {
                threadRxMonitor.Abort();
            }
        }

        private void ProcMonitorRx()
        {

            try
            {
                HostMachine machine = MachineManager.GetMachine();// 
                FrameManager frameManager = machine.FrameManager;

                FrameFilter filter = frameManager.CreateFilter();

                StringBuilder sb = new StringBuilder();


                while (true)
                {
                    Frame frame = filter.GetFrame(-1);
                    if (frame != null)
                    {
                        sb.Length = 0;

                        byte[] data=frame.RawData;
                        for (int i = 0; i < data.Length; i++)
                        {
                            sb.Append(data[i].ToString("X2")+" ");
                        }

                        sb.AppendLine();

                        this.Invoke(new MethodInvoker(delegate {

                            textBox1.AppendText(sb.ToString());
                        }));
                    }
                }
            }
            catch
            {
 
            }
           
        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
        }

    }
}
